﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace YougeBookmark.Models
{
    /// <summary>
    /// 标签实体类
    /// 对应数据库Tags表
    /// </summary>
    [Table("Tags")]
    public class Tag
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Required(ErrorMessage = "标签名称不能为空")]
        [Column(TypeName = "TEXT")]
        [MaxLength(100, ErrorMessage = "标签名称长度不能超过100字符")]
        [Index(IsUnique = true)]  // 通过迁移生成唯一约束
        public string Name { get; set; }

        [Required]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public DateTime CreatedAt { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]  // 需要数据库触发器支持更新
        public DateTime? ModifiedAt { get; set; }

        // 导航属性 - 与书签的关联关系
        public virtual ICollection<BookmarkTag> BookmarkTags { get; set; } = new HashSet<BookmarkTag>();

        /// <summary>
        /// 获取关联书签数量的快捷属性（不映射到数据库）
        /// </summary>
        [NotMapped]
        public int BookmarkCount => BookmarkTags?.Count ?? 0;
    }
}